﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Temp_20190707_AForm
{
    public class DockTabGroup : System.Windows.Forms.Panel
    {
        public DockTabGroup(DockPanel dockPanel)
        {
            this.m_DockPanel = dockPanel;
            if (dockPanel != null) dockPanel.ListTabGroup.Add(this);
            base.BackColor = Color.CornflowerBlue;
            base.Padding = this.Padding;
        }

        private const int CAPTION_HEIGHT = 22;
        private const int CAPTION_BTN_SIZE = 18;

        private bool m_Riveted = true;
        private DockPanel m_DockPanel;
        private DockAlignment m_Alignment = DockAlignment.Left;
        public virtual DockAlignment Alignment
        {
            get { return m_Alignment; }
            set { m_Alignment = value; }
        }
        public DockPanel DockPanel
        {
            get { return m_DockPanel; }
        }

        public virtual bool Riveted
        {
            get { return m_Riveted; }
            set
            {
                if (m_Riveted == value) return;
                m_Riveted = value;
                OnRivetChanged(EventArgs.Empty);
            }
        }

        public virtual Rectangle RectTitle
        {
            get { return new Rectangle(0, 0, this.Width, CAPTION_HEIGHT); }
        }
        public virtual Rectangle RectClose
        {
            get
            {
                Rectangle rect = RectTitle;
                return new Rectangle(rect.Width - CAPTION_BTN_SIZE - (rect.Height - CAPTION_BTN_SIZE) / 2, (rect.Height - 18) / 2, CAPTION_BTN_SIZE, CAPTION_BTN_SIZE);
            }
        }
        public virtual Rectangle RectRivet
        {
            get
            {
                Rectangle rect = RectClose;
                return new Rectangle(rect.X - 3 - rect.Width, rect.Y, rect.Width, rect.Height);
            }
        }



        protected object m_EventCloseClick = new object();
        protected object m_EventRivetClick = new object();
        protected object m_EventRivetChanged = new object();
        public event EventHandler CloseClick
        {
            add { base.Events.AddHandler(m_EventCloseClick, value); }
            remove { base.Events.RemoveHandler(m_EventCloseClick, value); }
        }
        public event EventHandler RivetClick
        {
            add { base.Events.AddHandler(m_EventRivetClick, value); }
            remove { base.Events.RemoveHandler(m_EventRivetClick, value); }
        }
        public event EventHandler RivetChanged
        {
            add { base.Events.AddHandler(m_EventRivetChanged, value); }
            remove { base.Events.RemoveHandler(m_EventRivetChanged, value); }
        }



        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Padding Padding
        {
            get { return new Padding(0, CAPTION_HEIGHT, 0, 0); }
        }
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected override Padding DefaultPadding
        {
            get { return this.Padding; }
        }

        public SplitterPanel ParentSplitPanel
        {
            get
            {
                var splitPanel = this.Parent as SplitterPanel;
                return splitPanel;
            }
        }
        public DockSplitControl ParentSplitControl
        {
            get
            {
                var parent = this.ParentSplitPanel;
                var splitCtrl = (parent != null) ? parent.Parent as DockSplitControl : null;
                return splitCtrl;
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.Refresh();
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            this.Refresh();
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.Refresh();
        }
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.Refresh();
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.Refresh();
        }
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);

            Point point1 = Control.MousePosition;
            Point point2 = this.PointToClient(point1);
            if (RectClose.Contains(point2)) OnCloseClick(e);
            else if (RectRivet.Contains(point2)) OnRivetClick(e);

        }
        protected virtual void OnCloseClick(EventArgs e)
        {
            EventHandler handler = base.Events[m_EventCloseClick] as EventHandler;
            if (handler != null) handler(this, e);
            this.DoClose();
        }
        protected virtual void OnRivetClick(EventArgs e)
        {
            EventHandler handler = base.Events[m_EventRivetClick] as EventHandler;
            if (handler != null) handler(this, e);
            this.DoRivet();
        }
        protected virtual void OnRivetChanged(EventArgs e)
        {
            EventHandler handler = base.Events[m_EventRivetChanged] as EventHandler;
            if (handler != null) handler(this, e);
            this.DoRivetChanged();
        }


        public virtual void DoClose()
        {
            var splitCtrl = this.ParentSplitControl;
            this.Parent = null;
            if (splitCtrl != null)
            {
                splitCtrl.DoLayout();
                if (m_DockPanel != null) m_DockPanel.DoLayout();
            }
            this.Dispose();
        }
        public virtual void DoRivet()
        {
            this.Riveted = !this.Riveted;
        }
        public virtual void DoRivetChanged()
        {
            var splitCtrl = this.ParentSplitControl;
            if (splitCtrl != null)
            {
                splitCtrl.DoLayout();
                if (m_DockPanel != null) m_DockPanel.DoLayout();
            }
        }

        public virtual new void Dispose()
        {
            this.Parent = null;
            this.m_DockPanel = null;
            base.Dispose();
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;

            g.FillRectangle(new SolidBrush(Color.RosyBrown), RectTitle);

            Point point1 = Control.MousePosition;
            Point point2 = this.PointToClient(point1);
            bool press = MouseButtons == MouseButtons.Left;
            Color colorClose = !RectClose.Contains(point2) ? Color.Coral : (press ? Color.Brown : Color.Chocolate);
            Color colorRivet = !RectRivet.Contains(point2) ? Color.LightGreen : (press ? Color.LimeGreen : Color.SeaGreen); 

            g.FillRectangle(new SolidBrush(colorClose), RectClose);
            g.FillRectangle(new SolidBrush(colorRivet), RectRivet);

        }
    }
}
